package com.hliushi.exception;

import com.google.common.collect.ImmutableMap;
import com.hliushi.response.CommonCode;
import com.hliushi.response.JsonResult;
import com.hliushi.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @ClassName GlobalExceptionHandler
 * @Description TODO
 * @Author liushi
 * @Date 2020/9/16 22:25
 * @Version V1.0
 **/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    // 打印log
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    // 记录map的builder对象,去构建ImmutableMap
    protected static ImmutableMap.Builder<Class<? extends Throwable>, CommonCode> builder = ImmutableMap.builder();
    //定义map,配置异常类型所对应的错误代码
    private static ImmutableMap<Class<? extends Throwable>, CommonCode> EXCEPTIONS;

    static {
        // 定义异常类型所对应的错误代码
        builder.put(HttpMessageNotReadableException.class, CommonCode.PARAMETER_EXCEPTION);
    }

    // 处理数据缺失异常
   /* @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public JsonResult handleHttpMessNotReadableException(MissingServletRequestParameterException ex) {
        LOGGER.error("缺少请求参数: {}", ex.getMessage());
        return new JsonResult("400", "缺少必要的请求参数");
    }*/


    /**
     * 空指针异常
     *
     * @param ex NullPointerException
     * @return
     */
   /* @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleTypeMismatchException(NullPointerException ex) {
        LOGGER.error("空指针异常: {}", ex.getMessage());
        return new JsonResult("500", "空指针异常了");
    }*/

    /**
     * 拦截自定义业务异常,返回业务类型信息
     */
    @ExceptionHandler(BusinessErrorException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleBusinessError(BusinessErrorException ex) {
        // 记录日志
        LOGGER.error("catch exception: {}", ex.getMessage());
        ResultCode resultCode = ex.getResultCode();
        // return new JsonResult("", resultCode);
        return new JsonResult(resultCode);
        // return new JsonResult("", "BusinessErrorException");
    }

    /**
     * 捕捉系统异常,预期以外异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult handleUnexpectedServer(Exception ex) {
        // 记录日志
        LOGGER.error("系统异常: {}", ex.getMessage());
        if (EXCEPTIONS == null) {
            // EXCEPTIONS构建成功
            EXCEPTIONS = builder.build();
        }
        // 从EXCEPTIONS中找异常类型所对应的错误代码,如果找到了将错误代码响应给用户,如果找不到给用户响应99999
        ResultCode resultCode = EXCEPTIONS.get(ex.getClass());
        if (resultCode != null) {
            // return new JsonResult("", resultCode);
            return new JsonResult(resultCode);
        } else {
            // 返回99999异常状态吗
            // return new JsonResult("", "Exception");
            // return new JsonResult("", CommonCode.UNEXCEPTION_EXCEPTION);
            return new JsonResult(CommonCode.UNEXCEPTION_EXCEPTION);
        }
    }
}
